# -*- coding: utf-8 -*- 

import logging
import datetime
from google.appengine.ext import db

def _today():
    now = _now()
    return datetime.date(now.year, now.month, now.day)
def _now():
    return datetime.datetime.utcnow() + datetime.timedelta(hours=+8)

class ShareCategory(db.Model):
    name = db.StringProperty()
    title = db.StringProperty()
    description = db.StringProperty(multiline=True)

class ShareLink(db.Model):
    user = db.UserProperty()

    name = db.StringProperty()
    title = db.StringProperty(multiline=True)
    description = db.StringProperty(multiline=True)

    date = db.DateTimeProperty(auto_now_add=True)
    picture = db.LinkProperty()
    link = db.LinkProperty()
    
    view_count = db.IntegerProperty(default=0)

    def create(self):
        self.date = _today()
        self.put()

    @property
    def title_short(self):
        if len(self.title) > 30:
            return "%s..."%self.title[:30]
        else:
            return self.title

    @property
    def title_long(self):
        if len(self.title) > 80:
            return "%s..."%self.title[:80]
        else:
            return self.title

    @property
    def date_rfc(self):
        return self.date.strftime("%a, %d %b %Y %H:%M:%S +0000")
    
    def increase_view_count(self):
        self.view_count += 1
        self.put()

    @property
    def date_rfc(self):
        return self.date.strftime("%a, %d %b %Y %H:%M:%S +0000")

class ShareCategoryLink(db.Model): 
    category = db.ReferenceProperty(ShareCategory, required=True,
      collection_name='category_links')
    link = db.ReferenceProperty(ShareLink, required=True,
      collection_name='link_categories')

class ShareLinkComment(db.Model):
    user = db.UserProperty()
    description = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)
    link = db.ReferenceProperty(ShareLink, required=True, collection_name='comments')

